#
#
# NOTE: Assumes that PERL5LIB and the COUNTER_ vars are set in the
# environment!!!!!!
#
COUNTER=-h $(COUNTER_DBHOST) $(COUNTER_DBNAME)
HTTPDLOG=/var/log/httpd
# SQL string for country comparisions
PICK=select country from temp_short where users >= 2 and not (country like "X%") order by userdensity

default:
	echo "No default target"
	echo "DB is $(COUNTER)"
	echo "Counter_Root is $(COUNTER_ROOT)"
	echo "Counter_Host is $(COUNTER_HOST)"
	echo "Counter_DBHost is $(COUNTER_DBHOST)"
	echo "Counter_DBName is $(COUNTER_DBNAME)"

# Don't run on i18n site now, but don't remove either
# Stuff generated every day
#stats: reports/byday.png reports/wwwbyday.png reports/visitors.png reports/storybyday.png hourly temp/countrydata.pl
# DON'T Removc

# Stuff done twice a day RUN WITHOUT cm-list on Gimel !!
# reports: reports/short.txt reports/machines.txt reports/short.php reports/machines.php reports/this-month.php cm-list www/smallplot.png
reports: reports/short.txt reports/machines.txt reports/short.php reports/machines.php reports/this-month.php www/smallplot.png

# also "counts" is run every day

# Don't use hourly for i18n now, but don't remove either
# Stuff generated every hour
# hourly: reports/byhour.png reports/wwwbyhour.png reports/visitorshour.png
# DON'T Remove

# Stuff generated every week
weekly: trends

# TO BE REMOVED
# Real specs....

#www/referers.html: $(HTTPDLOG)/referer_log writers/list-referers
#	grep -v -h 'Referer:http://counter.li.org' $(HTTPDLOG)/referer_log.1 \
#        $(HTTPDLOG)/referer_log | writers/list-referers -bypage -mergesearch > www/referers.php
# END TO BE REMOVED

#
# Make the counts that the reports are generated from
# Note the trickery to ensure that counts are NOT regenerated whenever
# I run "make reports", but that all reports are rewritten after
# I run "make counts"....
# 

# Don't use "counts" for now. We used the files created for main site!!
# Don't however remove this part
# counts: temp/persons.count temp/machines.count temp/country.story temp/started.story temp/persons.tab
	# Weakness in alpha TOUCH program
#	rm -f temp/countedflag
#	touch temp/countedflag
#
#temp/persons.count: db/persons.txt tools/countclean
#	echo Perl5lib = $(PERL5LIB) = $$PERL5LIB
#	tools/countclean db/persons country usage started > temp/persons.count.new
#	mv -f temp/persons.count.new temp/persons.count
#
#temp/machines.count: db/machines.txt tools/countclean
#	tools/countclean db/machines country accounts cpu cpu/ disk distribution mailer memory network source/ users purpose > temp/machines.count.new
#	mv -f temp/machines.count.new temp/machines.count
#
#temp/country.story: db/persons.txt
	#tools/fieldstory > temp/country.story.new
	# MUCH faster, but not so much tested
#	stats/db2story > temp/country.story.new
#	mv -f temp/country.story.new temp/country.story
#
#temp/started.story: temp/persons.count tools/plotstarttime
#	tools/plotstarttime temp/persons.count > temp/started.story.new
#	mv -f temp/started.story.new temp/started.story
# DON'T Remove

#
# Make the reports
#

reports/short.txt: ../temp/countedflag writers/writeshort
	# Generate the population counts from the database
	echo 'select name, longname, population from places where type = "country"' | mysql -s $(COUNTER) -ureader -preader > data/population.new
	mv -f data/population.new data/population
	writers/writeshort ../temp/persons.count ../temp/machines.count > reports/short.txt

reports/machines.txt: ../temp/countedflag writers/writemachines
	writers/writemachines ../temp/machines.count > reports/machines.txt

reports/short.php: ../temp/countedflag webmaint/writeshort.php www/reports/started.png reports/all-users.png
	webmaint/writeshort.php > reports/short.php.new
	mv -f reports/short.php.new reports/short.php

reports/machines.php: ../temp/countedflag writers/writemachines
	writers/writemachines -php ../temp/machines.count > reports/machines.php

www/smallplot.png: tools/plothistory ../temp/countedflag
	tools/plothistory --png --size 0.6 Real < ../temp/country.story | gnuplot > www/smallplot.png.new
	mv -f www/smallplot.png.new www/smallplot.png

reports/all-users.png: ../temp/countedflag tools/plothistory
	tools/plothistory --png All Real < ../temp/country.story | gnuplot > www/reports/all-users.png.new
	mv -f www/reports/all-users.png.new www/reports/all-users.png

www/reports/started.png: ../temp/countedflag tools/plothistory
	grep All ../temp/country.story | sed -e 's/:All:/:Registered:/'| cat - ../temp/started.story | sort > temp/startreg.story
	tools/plothistory --png -d -f 1992-01-01 Started Registered < temp/startreg.story | gnuplot > www/reports/started.png.new
	mv -f www/reports/started.png.new www/reports/started.png

bycountry: www/bycountry/index.php

www/bycountry/index.php: www/bycountry/ALl.php

www/bycountry/ALl.php: ../temp/personlists.flag writers/countryinfo ../temp/countrydata.pl
	writers/countryinfo -R -prefix www/bycountry 1
	cp www/bycountry/ALl.php www/bycountry/index.php

reports/this-month.php: ../temp/countedflag writers/this-month
	writers/this-month ../temp/country.story > reports/this-month.php
#
# Tabulations used for generating the users list
# NOTE: It is now ONLY used by the "grep" function, and should go away
# as soon as we have a proper search dialog!
#
temp/persons.tab: db/persons.txt tools/tabulate
	tools/tabulate db/persons country state city name email may-publish | writers/tidypersons | sort -u -f +1 > temp/persons.tab

# Traffic statistics

reports/byday.png: temp/byday.prn tools/plotbyday
	rm -f reports/byday.png.new
	gnuplot tools/plotbyday > reports/byday.png.new
	mv -f reports/byday.png.new reports/byday.png

temp/byday.prn: tools/byday logs/eventlog
	tools/byday -days 60 -to `date --utc +%Y-%m-%dT00` logs/eventlog > temp/byday.prn.new
	mv -f temp/byday.prn.new temp/byday.prn

reports/byhour.png: ../temp/byhour.prn tools/plotbyhour
	rm -f reports/byhour.png.new
	gnuplot tools/plotbyhour > reports/byhour.png.new
	mv -f reports/byhour.png.new reports/byhour.png

temp/byhour.prn: tools/byday logs/eventlog
	tools/byday -byhour -days 3 -to `date --utc +%Y-%m-%dT%H` -lines 15000 > temp/byhour.prn.new
	rm -f temp/byhour.prn
	mv temp/byhour.prn.new temp/byhour.prn

reports/wwwbyday.png: temp/wwwbyday.prn tools/plotwww
	gnuplot tools/plotwww > reports/wwwbyday.png

temp/wwwbyday.prn: tools/wwwbyday $(HTTPDLOG)/access_log.2 $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log
	tools/wwwbyday -days 30 -to `date --utc +%Y-%m-%dT00` $(HTTPDLOG)/access_log.3 $(HTTPDLOG)/access_log.2 $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log > temp/wwwbyday.prn

reports/visitors.png: temp/visitors.prn tools/plotwww
	gnuplot tools/plotvisitors > reports/visitors.png

temp/visitors.prn: tools/wwwvisitorsbyday $(HTTPDLOG)/access_log.2 $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log
	tools/wwwvisitorsbyday -days 30 -to `date --utc +%Y-%m-%dT00` $(HTTPDLOG)/access_log.4 $(HTTPDLOG)/access_log.3 $(HTTPDLOG)/access_log.2 $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log > temp/visitors.prn

reports/storybyday.png: temp/storybyday.prn
	tools/plotratebycountry -onemonth < temp/storybyday.prn | gnuplot > reports/storybyday.png
temp/storybyday.prn: db/persons.txt tools/storybyday
	tools/storybyday > temp/storybyday.prn

reports/visitorshour.png: temp/visitorshour.prn tools/plotwww
	gnuplot tools/plotvisitorsbyhour > reports/visitorshour.png.new
	mv -f reports/visitorshour.png.new reports/visitorshour.png

temp/visitorshour.prn: tools/wwwvisitorsbyday $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log
	tools/wwwvisitorsbyday -days 3 -to `date --utc +%Y-%m-%dT%H` -byhour $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log > temp/visitorshour.prn

reports/wwwbyhour.png: temp/wwwbyhour.prn tools/plotwwwbyhour
	rm -f reports/wwwbyhour.png.new
	gnuplot tools/plotwwwbyhour > reports/wwwbyhour.png.new
	mv -f reports/wwwbyhour.png.new reports/wwwbyhour.png

temp/wwwbyhour.prn: tools/wwwbyday /var/log/httpd/access_log.1 /var/log/httpd/access_log
	tools/wwwbyday -to `date --utc +%Y-%m-%dT%H` -byhour -days 3 $(HTTPDLOG)/access_log.1 $(HTTPDLOG)/access_log > temp/wwwbyhour.prn

# The do()able file of per-country data
temp/countrydata.pl: tools/makedata data/population data/hostcount ../temp/countedflag
	tools/makedata > temp/countrydata.pl.new
	mv temp/countrydata.pl.new temp/countrydata.pl


# The history files of relative growth
# Have to mish the IDs in by hand
trends: www/reports/firstfive.png www/reports/secondfive.png www/reports/thirdfive.png www/reports/fourthfive.png www/reports/fifthfive.png www/reports/lastfive.png
www/reports/firstfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) desc limit 5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/firstfive.png

www/reports/secondfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) desc limit 5,5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/secondfive.png

www/reports/thirdfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) desc limit 10,5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/thirdfive.png

www/reports/fourthfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) desc limit 15,5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/fourthfive.png

www/reports/fifthfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) desc limit 20,5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/fifthfive.png

www/reports/lastfive.png: ../temp/countedflag Makefile 
	tools/plothistory --png -p `echo '$(PICK) limit 5' | mysql -s $(COUNTER) -ureader -preader` < ../temp/country.story | gnuplot > www/reports/lastfive.png

#
# Data relevant to country managers
#
managing: reports/maintainers.php temp/personlists.flag temp/placelists.flag

reports/maintainers.php: db/places.txt writers/listmaintainers
	writers/listmaintainers > reports/maintainers.new.php
	mv reports/maintainers.new.php reports/maintainers.php

temp/personlists.flag: ../temp/countedflag tools/recount tools/homeless
	# Must make sure old aliases get cleaned
	tools/homeless | tools/showclean -filter -w
	# Recount all the places
	tools/recount -r -a All
	rm -f temp/personlists.flag
	touch temp/personlists.flag

temp/placelists.flag: db/places.txt
	#tools/makeplacenet
	rm -f temp/placelists.flag
	touch temp/placelists.flag

# Maintain linux country managers list
cm-list: /var/majordomo/lists/country-managers

/var/majordomo/lists/country-managers: db/places.txt
	tools/listmaintainers > /var/majordomo/lists/country-managers

www/adm/aliases.txt: db/places.txt
	aliaslist -l | sort -f > www/adm/aliases.txt

permissions:
	touch db/users.txt
	touch db/persons.txt
	touch db/machines.txt
	touch db/places.txt
	touch db/friends.txt
#	touch db/usergroups.txt

	chgrp -R counter db
	chgrp counter cgi-bin/adm cgi-bin/runscript
	chmod g+s cgi-bin/adm cgi-bin/runscript
	chmod -R g+w db
	touch logs/eventlog
	chgrp counter logs/eventlog
	chmod g+w logs/eventlog

# Get data from external sources
gather: temp/population.txt

temp/population.txt: ALWAYS
	wget http://www.un.org/Depts/unsd/social/population.htm -O temp/population.html
	lynx -dump temp/population.html > temp/population.txt

update-population: temp/population.txt
	enterpopulation temp/population.txt

# ----------------- TOOLS TO BE USED WHEN CLONING/TESTING THE COUNTER -------
#
# Make sure rebuilding is done in proper order
#
rebuild:
	tools/maintain -c rebuild db/places
	tools/maintain -c rebuild db/users
	tools/maintain -c rebuild db/persons
	tools/maintain -c rebuild db/machines
	tools/maintain -c rebuild db/friends
	#tools/maintain -c rebuild db/usergroups
	tools/maintain -c rebuild db/email

#
# For testing purposes: Make everything
#
everything: directories counts reports bycountry managing hourly stats

# Creating missing logfiles (empty)

logs/eventlog:
	touch logs/eventlog
	echo ">>>> Empty log created" >> logs/eventlog

directories: logs temp reports/.
	echo "Directories created"

logs:
	mkdir logs
temp:
	mkdir temp
reports/.:
	# reports are publicly visible, and therefore a symlink into
	# the web directory
	ln -s www/reports .

db/persons.txt:
	touch db/persons.txt
db/users.txt:
	touch db/users.txt
db/machines.txt:
	touch db/machines.txt
db/friends.txt:
	touch db/friends.txt
#db/usergroups.txt:
#	touch db/usergroups.txt

# Tricks: ALWAYS will always evaluate unless there's a file named
# ALWAYS in the current directory...

ALWAYS:
